use crate::dto::{
    match_no_res_ok, match_ok,
    post::{SysPostList, SysPostListPayload, SysPostModifyPayload},
    Page, Res, ResObj,
};
use crate::service::post_service;
use hypers::prelude::*;
pub struct Post;

#[openapi(prefix = "/system", name = "/post", tag = "岗位")]
impl Post {
    /// 岗位列表
    #[get(
        "/list",
        parameter(
            SysPostListPayload
        ),
        response(
            (status = 200,body=ResObj<Page<SysPostList>>,description ="岗位列表")
        ),
    )]
    async fn get_post_page(payload: SysPostListPayload) -> Res<Page<SysPostList>> {
        match_ok(
            post_service::get_post_page(
                payload.page_num,
                payload.page_size,
                payload.post_code,
                payload.post_name,
                payload.status,
            )
            .await,
        )
    }

    /// 添加岗位
    #[post(
        "/",
        response(
            (status = 200,body=ResObj<()>,description ="添加岗位")
        ),
    )]
    async fn post_add_post(payload: Json<SysPostModifyPayload>, req: &mut Request) -> Res<()> {
        let user_id = req.get::<i32>("userId").unwrap();
        match_no_res_ok(
            post_service::post_add_post(
                user_id,
                payload.post_code.clone(),
                payload.post_name.clone(),
                payload.post_sort,
                payload.status.clone(),
                payload.remark.clone(),
            )
            .await,
        )
    }

    /// 修改岗位
    #[put(
        "/",
        response(
            (status = 200,body=ResObj<()>,description ="修改岗位")
        ),
    )]
    async fn put_edit_post(payload: Json<SysPostModifyPayload>, req: &mut Request) -> Res<()> {
        let user_id = req.get::<i32>("userId").unwrap();
        match_no_res_ok(
            post_service::post_edit_post(
                user_id,
                payload.post_id,
                payload.post_code.clone(),
                payload.post_name.clone(),
                payload.post_sort,
                payload.status.clone(),
                payload.remark.clone(),
            )
            .await,
        )
    }

    /// 岗位详情
    #[get(
        "/:id",
        response(
            (status = 200,body=ResObj<Option<SysPostList>>,description ="岗位详情")
        ),
    )]
    async fn get_post_by_id(id: Path<i64>) -> Res<Option<SysPostList>> {
        match_ok(post_service::get_post_by_id(id.0).await)
    }

    /// 删除岗位
    #[delete(
        "/:id",
        response(
            (status = 200,body=ResObj<()>,description ="删除岗位")
        ),
    )]
    async fn del_post_by_id(id: Path<String>) -> Res<()> {
        match_no_res_ok(post_service::del_post_by_id(id.0).await)
    }
}
